define(function(require, exports, module) {
    "use strict";
    var $ = require("$"),
        app = require("dist/application/app"),
        config = app.config,
        method = app.method;
    exports.init = function(form) {
        var $color = $(".color", form);
        $color.length && (function() {
            require.async(["jscolor"], function(jscolor) {
                jscolor.installd();
            });
        })();
        var $rangeSlider = $('[data-toggle="rangeSlider"]');
        $rangeSlider.length && (function() {
            require.async(["rangeSlider", "assets/stylesheets/plugins/rangeSlider/css/ion.rangeSlider.css", "assets/stylesheets/plugins/rangeSlider/css/ion.rangeSlider.skinNice.css"], function() {
                $rangeSlider.each(function() {
                    var $this = $(this),
                        hideminmax = $this.data("hideMinMax") == false ? false : true,
                        min = $this.data("min") || 0,
                        max = $this.data("max") || 1000,
                        from = $this.data("from") || 0,
                        step = $this.data("step") || 1,
                        postfix = $this.data("postfix") || "",
                        grid = $this.data("grid") == false ? false : true,
                        to = $this.data("to") || max,
                        type = $this.data("type") || "single",
                        kstep = $this.data("kstepPercent") || 0.5,
                        kboard = $this.data("keyboard") == false ? false : true;
                    $this.ionRangeSlider({
                        hide_min_max: hideminmax,
                        keyboard: kboard,
                        keyboard_step: kstep,
                        min: min,
                        max: max,
                        from: from,
                        to: to,
                        type: type,
                        step: step,
                        postfix: postfix,
                        grid: grid
                    });
                });

            });
        })();
        var $js_blockitems = $(".js_blockitems",form);
        $js_blockitems.length && (function() {
            require.async(["kindeditor", "assets/kindeditor/themes/default/default.css"], function() {
                var editor = KindEditor.editor({
                    allowFileManager: true
                });
                $(form).on("click", ".thumb-select", function(e) {
                    var $this = $(this),
                        $hide_input = $this.find("input[type=text]"),
                        $img = $this.find("img");

                    var imagePath = $hide_input.val();
                    editor.loadPlugin('image', function() {
                        editor.plugin.imageDialog({
                            imageUrl: imagePath,
                            clickFn: function(url, title, width, height, border, align) {
                                $hide_input.val(url).hide();
                                if ($img.length > 0) {
                                    $img.attr('src', url).removeClass("hide")
                                }
                                $this.addClass('selected');
                                $img.removeClass('thumb-default-img')
                                editor.hideDialog();
                            }
                        });
                    });
                });
                $(form).on("click", '[data-toggle="selectimg2"]', function(e) {
                    var $this = $(e.target),
                        $hide_input = $this.prevAll("input[type=text]"),
                        $text_input = $this.closest('div.input-group').find("input[type=text]"),
                        insert = $this.data("insert"),
                        input = $this.data("input");
                    var imagePath = $hide_input.val();
                    input && (imagePath = $text_input.val());
                    editor.loadPlugin('image', function() {
                        editor.plugin.imageDialog({
                            imageUrl: imagePath,
                            clickFn: function(url, title, width, height, border, align) {
                                if (insert || insert == undefined) {
                                    $hide_input.val(url).hide();
                                    var timg = $this.prevAll("img"),
                                        $target = $($this.data("target"));
                                    if ($target.length > 0) {
                                        $target.attr('src', url).removeClass("hide");
                                    } else {
                                        if (timg.length > 0) {
                                            timg.attr('src', url).removeClass("hide");
                                        } else {
                                            var tmp = '<img class="thumb_img" src="{0}" style="max-height: 100px;">';
                                            $hide_input.before(tmp.format(url))
                                        }
                                    }

                                } else {
                                    $this.trigger('insert', url);
                                }
                                input && ($text_input.val(url));
                                $this.trigger('callback', url);
                                editor.hideDialog();
                            }
                        });
                    });
                });
            });
            $(form).on("click", ".js_blockitems_add", function() {
                var $blockitems = $(".js_blockitems",$(form)),
                    count = $blockitems.find('.blockitem').length;
                if (count < 10) {
                    $(".js_blockitems", form).append(template("blockitem_tmp", {
                        index: count
                    }))
                } else {
                    config.msg.info(config.lang.maxItem10)
                }

            });
            $(form).on("click", ".js_blockitems_add_float", function() {
                var $this = $(this),
                    $style = $this.closest('.js_floatmenu_set_style'),
                    $blockitems = $(".js_blockitems", $style),
                    count = $blockitems.find('.blockitem').length,
                    tempid = $this.data("tempid"),
                    max = $this.data("max"),
                    msg = $this.data("msg");
                if (max) {
                    if (count < max) {
                        $(".js_blockitems", $style).append(template(tempid, {
                            index: count
                        }));
                    } else {
                        config.msg.info(msg || config.lang.maxNumLength.format(max))
                    }
                }else{
                    $(".js_blockitems", $style).append(template(tempid, {
                        index: count
                    }));
                }
                var $color = $(".color", form);
                $color.length && (function() {
                    require.async(["jscolor"], function(jscolor) {
                        jscolor.installd();
                    });
                })();
            });
            $(form).on("click", ".js_blockitems_add_multy", function() {
                var $this = $(this),
                    $style = $this.closest('.form-group'),
                    $blockitems = $(".js_blockitems", $style),
                    count = $blockitems.find('.js_fblockitem').length,
                    tempid = $this.data("tempid"),
                    max = $this.data("max"),
                    msg = $this.data("msg");
                if (max) {
                    if (count < max) {
                        $(".js_blockitems", $style).append(template(tempid, {
                            index: count,
                            id:new Date().getTime()
                        }));
                    } else {
                        config.msg.info(msg || config.lang.maxNumLength.format(max))
                    }
                } else {
                    $(".js_blockitems", $style).append(template(tempid, {
                        index: count,
                        id:new Date().getTime()
                    }));
                }
                var $color = $(".color", form);
                $color.length && (function() {
                    require.async(["jscolor"], function(jscolor) {
                        jscolor.installd();
                    });
                })();
            });
            $(form).on("click", ".js_blockitems_add_smulty", function() {
                var $this = $(this),
                    $style = $this.closest('.form-group'),
                    $blockitems = $(".js_blockitems_second", $style),
                    count = $blockitems.find('.js_sblockitem').length,
                    tempid = $this.data("tempid"),
                    max = $this.data("max"),
                    msg = $this.data("msg"),
                    id=$this.closest('.js_fblockitem').data("id"),
                    findex=$this.closest('.js_fblockitem').data("index");
                if (max) {
                    if (count < max) {
                        $(".js_blockitems_second", $style).append(template(tempid, {
                            index: count,
                            findex:findex,
                            id:id
                        }));
                        tipsSwitch($this,true);
                    } else {
                        config.msg.info(msg || config.lang.maxNumLength.format(max))
                    }
                } else {
                    $(".js_blockitems_second", $style).append(template(tempid, {
                        index: count,
                        id:id
                    }));
                    tipsSwitch($this,true);
                }
                var $js_sortable = $(".js_sortable",$style);
                $js_sortable.length && (function() {
                    $js_sortable.sortable()
                })()
                var $color = $(".color", form);
                $color.length && (function() {
                    require.async(["jscolor"], function(jscolor) {
                        jscolor.installd();
                    });
                })();
            });
            function tipsSwitch(obj,flag){
                var $js_fblockitem=obj.closest('.js_fblockitem'),
                $line=$(".js_line_first_select",$js_fblockitem),$tips=$(".js_line_first_tips",$js_fblockitem);
                if(flag){
                    $line.addClass('hide');
                    $tips.removeClass('hide');
                }else{
                    $tips.addClass('hide');
                    $line.removeClass('hide');
                }
            }
            $(form).on("change", ".js_nav_type", function() {
                var selected = $(this).find('option:selected');
                var type = selected.data("type");
                var flag = (type == "inner");
                var blockitem = $(this).closest('.blockitem');
                blockitem.find("input.error").removeClass('error');
                blockitem.find("span.error").remove();
                $(".js_nav_page", blockitem).toggle(flag);
                $(".js_nav_text", blockitem).toggle(!flag);
            });
            $(form).on("click", '.js_del_group', function(e) {
                var $this = $(this);
                var $group = $this.closest('.blockitem');
                if ($group.siblings().length > 0) {
                    var confirm = $group.find(".selected").length > 0;

                    var done = function() {
                        $group.fadeOut(function() {
                            $group.remove();
                        });
                    }
                    confirm && config.msg.msgbox.confirm(config.lang.confirmRemove, function(result) {
                        if (result) {
                            done();
                        }
                    });
                    !confirm && done();
                } else {
                    config.msg.info(config.lang.minOneItem)
                }
            });
            $(form).on("click", '.js_del_group_float', function(e) {
                var $this = $(this),
                    $js_blockitems = $this.closest('.js_blockitems');
                var $group = $this.closest('.blockitem'),
                    min = $js_blockitems.data("min") || 1;
                if ($group.siblings().length > min - 1) {
                    var confirm = $group.find(".selected").length > 0;

                    var done = function() {
                        $group.fadeOut(function() {
                            $group.remove();
                        });
                    }
                    confirm && config.msg.msgbox.confirm(config.lang.confirmRemove, function(result) {
                        if (result) {
                            done();
                        }
                    });
                    !confirm && done();
                } else {
                    config.msg.info(config.lang.minNumItem.format(min))
                }
            });
            $(form).on("click", '.js_del_group_sfloat', function(e) {
                var $this = $(this),
                    $js_blockitems = $this.closest('.js_blockitems');
                var $group = $this.closest('.blockitem'),
                    min = $js_blockitems.data("min") || 0;
                if ($group.siblings().length > min - 1) {
                    var confirm = $group.find(".selected").length > 0;

                    var done = function() {
                        $group.fadeOut(function() {
                            $group.remove();
                        });
                    }
                    confirm && config.msg.msgbox.confirm(config.lang.confirmRemove, function(result) {
                        if (result) {
                            done();
                        }
                    });
                    !confirm && done();
                    if($group.siblings().length==0){
                        tipsSwitch($this,false);
                    }
                } else {
                    config.msg.info(config.lang.minNumItem.format(min))
                }
            });
        })();


        var $js_item_list = $(".js_item_list", form);
        $js_item_list.length && (function() {
            var $js_make_item = $(".js_make_item", $(".js_item_modal_fn"));
            var $js_add_item = $(".js_add_item", form);

            $js_add_item.on("click", function() {

                $(".js_item_modal_fn").find("input[type=checkbox]").prop("checked", false);
                $(".dd-item", $js_item_list).each(function() {
                    var $this = $(this),
                        val = $this.find("input[type=hidden]").val();
                    $(".js_item_modal_fn").find("input[type=checkbox][value=" + val + "]").prop("checked", true);
                });
                $(".js_item_modal_fn").modal("show");

            });

            var done = function() {
                $js_item_list.nestable({
                    maxDepth: 1
                });
                $('.dd-handle a', form).on('mousedown', function(e) {
                    e.stopPropagation();
                });
            }
            require.async("nestable", done);
            $js_make_item.on("click", function() {
                var $item = $('.js_item_modal_fn input[name="item"]:checked'),
                    len = $item.length;
                if ($item.length < 5) {
                    $("ol", $js_item_list).html("");
                    $item.each(function() {
                        var add_item_length = $(".dd-item", form).length,
                            $this = $(this);
                        var data = {
                            name: $this.data("name"),
                            val: $this.val(),
                            index: add_item_length,
                            id: add_item_length
                        };
                        $("ol", $js_item_list).append(template.compile(require('./template/item.html'))(data));
                    });

                    $(".js_item_modal_fn").modal("hide");
                    done();
                } else {
                    config.msg.info(config.lang.maxFnLength)
                }

            });
        })();
        var $js_classes_list = $(".js_classes_list", form);
        $js_classes_list.length && (function() {
            var $js_make_classes = $(".js_make_classes");
            var $js_add_classes_item = $(".js_add_classes_item", form),
                wid = form.attr("wid");
            $js_add_classes_item.on("click", function() {
                $("#js_classes_modal").find("input[type=checkbox]").prop("checked", false);
                $(".dd-item", $js_classes_list).each(function() {
                    var $this = $(this),
                        val = $this.find("input[type=hidden]").val();
                    $("#js_classes_modal").find("input[type=checkbox][value=" + val + "]").prop("checked", true);
                });
                $("#js_classes_modal").modal("show");

            });
            var done = function() {
                $js_classes_list.nestable({
                    maxDepth: 1
                });
                $('.dd-handle a', form).on('mousedown', function(e) {
                    e.stopPropagation();
                });
            }
            require.async("nestable", done);
            $js_make_classes.on("click", function() {
                var $item = $('#js_classes_modal input[name="item"]:checked'),
                    len = $item.length;

                $("ol", $js_classes_list).html("");
                $item.each(function() {
                    var add_item_length = $(".dd-item", form).length,
                        $this = $(this);
                    var data = {
                        name: $this.data("name"),
                        val: $this.val(),
                        type:$this.data("type"),
                        index: add_item_length,
                        id: add_item_length
                    };
                    $("ol", $js_classes_list).append(template("js_classes_html", data));
                });
                $("#js_classes_modal").modal("hide");
                done();
            });
        })();
        $(form).on("change", ".js_link_type", function() {
            var selected = $(this).find('option:selected');
            var type = selected.data("type");
            var flag = (type == "inner");
            var not = (type == "not");
            var row = $(this).closest('.row');
            row.find("input.error").removeClass('error');
            row.find("span.error").remove();
            not && $(".js_link_page", row).add(".js_link_text", row).hide()
            if (!not) {
                $(".js_link_page", row).toggle(flag);
                $(".js_link_text", row).toggle(!flag);
            };
        });

        var $select2 = $('[data-toggle="select2"]', form);
        $select2.length > 0 && require.async(['select2', 'style/plugins/select2/select2.min.css'], function() {
            $select2.each(function() {
                var t = $(this),
                    n = {},
                    c = t.data('maxcount'),
                    o = t.data('chang'),
                    r = t.data('remote') || t.attr('href');
                c && (n.maximumSelectionSize = c);
                if (o) {
                    switch (o) {
                        case "submit":
                            {
                                t.on("change", function(e) {
                                    t.closest("form ").submit();
                                })
                            }
                    }
                };
                t.select2(n)
            })
        });

        var $selecttype = $(".js_selecttype", form);
        if ($selecttype.length) {
            var $selecttypec = $(".js_selecttype:checked", form),
                type = $selecttypec.data("type");
            $(".js_selecttype_show", form).addClass('hide');
            $(".js_" + type, form).removeClass('hide');
            $selecttype.click(function() {
                var $this = $(this),
                    type = $this.data("type"),
                    flag = $this.prop("checked");
                if (flag) {
                    $(".js_selecttype_show", form).addClass('hide');
                    $(".js_" + type, form).removeClass('hide');
                }

            });
        };
        $(form).on("click", "[data-toggle='selectGoodss']", function() {
            var $this = $(this);
            require.async("dist/selectgoodsn/init", function(selectgoodsn) {
                selectgoodsn.init(form, $this);
            });
        });
        $(form).on("click", "[data-toggle='allLink']", function() {
            var $this = $(this);
            require.async("dist/link/init", function(link) {
                link.init(form, $this);
            });
        });
        var $icons=$("[data-toggle='icon']");
        $icons.length && (function(){
            require.async(["assets/stylesheets/plugins/icon/font-awesome.css","assets/stylesheets/plugins/icon/weimobfont.css"]);
        })();
        $(form).on("click", "[data-toggle='icon']", function() {
            var $this = $(this);
            require.async(["dist/icon/init","assets/stylesheets/plugins/icon/font-awesome.css","assets/stylesheets/plugins/icon/weimobfont.css"], function(icon) {
                icon.init(form, $this);
            });
        });
        $(form).on("click", "[data-toggle='activity']", function() {
            var $this = $(this);
            require.async("dist/activity/init", function(activity) {
                activity.init(form, $this);
            });
        });
        var $js_styles_select = $(".js_styles_select", form);
        $js_styles_select.length && (function() {
            var $style = $(".js_styles_select:checked", form),
                type = $style.data("type"),colorset=$style.data("colorset"),
                $js_title_link = $(".js_title_link", form),
                $js_title_link_input = $(".js_title_link_input", form),
                $js_link_address = $(".js_link_address", form),
                $js_title_color_set=$(".js_title_color_set",form);
            if (type == "link") {
                $js_title_link.removeClass('hide');
                if ($js_title_link_input.val().length > 0) {
                    $js_link_address.removeClass('hide');
                }
            }
            $js_title_color_set.addClass('hide');
            $(".js_color_set_"+colorset,form).removeClass('hide');
            $js_styles_select.click(function() {
                var type = $(this).data("type"),colorset = $(this).data("colorset");
                $js_title_color_set.addClass('hide');
                $(".js_color_set_"+colorset,form).removeClass('hide');
                if (type == "link") {
                    $js_title_link.removeClass('hide');
                    if ($js_title_link_input.val().length > 0) {
                        $js_link_address.removeClass('hide');
                    }
                } else {
                    $js_title_link.addClass('hide');
                    $js_link_address.addClass('hide');
                }
            });
            $js_title_link_input.keyup(function(event) {
                var val = $(this).val();
                if (val.length > 0) {
                    $js_link_address.removeClass('hide');
                } else {
                    $js_link_address.addClass('hide');
                }
            });
        })();

        var $js_auto_select = $(".js_auto_select", form);
        $js_auto_select.length && (function() {
            var $style = $(".js_auto_select option:selected", form),
                type = $style.data("type"),
                $js_auto_classes = $(".js_auto_classes", form),
                $js_auto_actity = $(".js_auto_actity", form);
            if (type == "classes") {
                $js_auto_classes.removeClass('hide');
                $js_auto_actity.addClass('hide');
            } else if (type == "actity") {
                $js_auto_classes.addClass('hide');
                $js_auto_actity.removeClass('hide');
            } else {
                $js_auto_classes.addClass('hide');
                $js_auto_actity.addClass('hide');
            }
            $js_auto_select.change(function() {
                var type = $(this).find("option:selected").data("type");
                if (type == "classes") {
                    $js_auto_classes.removeClass('hide');
                    $js_auto_actity.addClass('hide');
                } else if (type == "actity") {
                    $js_auto_classes.addClass('hide');
                    $js_auto_actity.removeClass('hide');
                } else {
                    $js_auto_classes.addClass('hide');
                    $js_auto_actity.addClass('hide');
                }
            });
        })();
        var $js_link_type_select = $(".js_link_type_select", form);
        $js_link_type_select.length && (function() {
            $(".js_link_type_select:checked", form).each(function() {
                var $this = $(this),
                    $p = $this.closest('.row'),
                    type = $this.data("type");
                $(".js_link_type_container", $p).addClass('hide');
                $(".js_link_type_" + type, $p).removeClass('hide');
            });
        })();
        $(form).on("click", ".js_link_type_select", function() {
            var $this = $(this),
                $p = $this.closest('.row'),
                type = $this.data("type");
            $(".js_link_type_container", $p).addClass('hide');
            $(".js_link_type_" + type, $p).removeClass('hide');
        });
        var $js_header_tyle_radio = $(".js_header_tyle input[type='radio']:checked", form);
        $js_header_tyle_radio.length && (function() {
            headerStyle($js_header_tyle_radio);
        })();
        $(form).on("click", ".js_header_tyle input[type='radio']", function() {
            headerStyle($(this));
        });

        function headerStyle(obj){
            var $this = obj,
                type = $this.data("type"),
                $bgcolor = $(".js_header_bgcolor", form),
                $logo = $(".js_header_logo", form),
                $bgimg = $(".js_header_bgimg", form);
            if (type) {
                var arr = type.split(",");
                $.each(arr, function() {
                    $(".js_header_" + this, form).removeClass('hide');
                });
                if(arr.length>1){
                    $("input[type='radio']", $bgimg).prop("disabled", false);
                }else{
                    $bgimg.addClass('hide');
                    $("input[type='radio']", $bgimg).prop("disabled", true);
                }
            } else {
                $logo.addClass('hide');
                $bgimg.addClass('hide');
                $("input[type='radio']", $bgimg).prop("disabled", true);
            }
        }
        var $js_floatmenu_style_radio = $(".js_floatmenu_style input[type='radio']:checked", form);
        $js_floatmenu_style_radio.length && (function() {
            floatStyle($js_floatmenu_style_radio);
        })();
        $(form).on("click", ".js_floatmenu_style input[type='radio']", function(e) {
            e.stopPropagation();
            floatStyle($(this));
        });
       
        function floatStyle(obj){
            var type=obj.data("type"),$hide=$(".js_floatmenu_set_style",form),sizes=obj.data("size"),
            size=[],sbtitles=[],$show=$(".js_floatmenu_set_style_"+type,form),$js_size_span=$(".js_size_span",$show),
            sbtitle=obj.data("subtitle"),$js_title_sb_span=$(".js_title_sub_span",$show);
            $hide.addClass('hide');
            $hide.find("input,select,textarea").attr("disabled",true).addClass('wizard-ignore');
            $show.removeClass('hide');
            $show.find("input,select,textarea").prop("disabled",false).removeClass('wizard-ignore');
            if(sizes){
                size= sizes.split(",");
            }
            if(sbtitle){
                sbtitles= sbtitle.split(",");
            }
            $js_title_sb_span.each(function(i) {
                $(this).html(sbtitles[i]);
            });
            if(size.length>1){
                $js_size_span.each(function(i){
                    $(this).html(size[i]);
                });
            }else{
                $js_size_span.html(size[0]);
            }
        }
        $(form).on("click",".js_act_select_page",function(){
            var $form=$(this).closest('form'),$js_page_url_activity=$(".js_page_url_activity",$form),
            $radio=$("input[type='radio']",$js_page_url_activity.closest('label'));
            if($radio.prop("checked") && !$js_page_url_activity.val().length){
                config.msg.error("请选择页面");
                return false;
            }
        });
        var $js_buy_switch_btn=$(".js_buy_switch_btn:checked",form);
        $js_buy_switch_btn.length && (function() {
            buyBtn($js_buy_switch_btn);
        })();
        $(form).on("click", ".js_buy_switch_btn", function() {
            buyBtn($(this));
        });
        var $js_page_sb_select=$(".js_page_sb_select:checked",form);
        $js_page_sb_select.length && (function() {
            pageSelectBtn($js_page_sb_select);
        })();
        $(form).on("click", ".js_page_sb_select", function() {
            pageSelectBtn($(this));
        });
        function buyBtn(obj){
            var flag=obj.prop("checked"),$js_buy_show_div=$(".js_buy_show_div",form);
            if(flag){
                $js_buy_show_div.removeClass("hide");
                $js_buy_show_div.find("input").attr("disabled",false).removeClass('wizard-ignore');
            }else{
                $js_buy_show_div.addClass("hide");
                $js_buy_show_div.find("input,select,textarea").prop("disabled",true).addClass('wizard-ignore');
            }
        }
        function pageSelectBtn(obj){
            var flag=obj.prop("checked"),type=obj.data("type"),
            $js_sb_page_custom=$(".js_sb_page_custom",form);
            if(type=="default"){
                $js_sb_page_custom.find("button").prop("disabled",true);
                $js_sb_page_custom.find("input").prop("disabled",true);
            }else{
                $js_sb_page_custom.find("button").prop("disabled",false);
                $js_sb_page_custom.find("input").prop("disabled",false);
            }
        }
    }


})